package net.pws.oos.web.dto;

import java.util.Date;

import net.pws.common.beanutils.BeanUtils;
import net.pws.common.persistence.EntityManager;
import net.pws.oos.biz.model.Indicator;
import net.pws.oos.biz.model.IndicatorValue;
import net.pws.oos.biz.model.Organization;
import net.pws.oos.biz.model.User;
import net.pws.oos.security.SecurityUtils;

import org.apache.commons.lang.StringUtils;

public class IndicatorValueDto extends IndicatorValue {
	/**
	 * 
	 */
	private static final long serialVersionUID = 3495880281905987723L;

	public static IndicatorValueDto from(IndicatorValue indicatorValue) {
        IndicatorValueDto result = new IndicatorValueDto();
        String[] props = {"indicator", "creator", "createOrganization"};
        BeanUtils.copyProperties(result, indicatorValue, props);
        Indicator indicator = indicatorValue.getIndicator();
        if (null != indicator) {
            result.setIndicatorId(indicator.getId());
            result.setIndicatorName(indicator.getName());
        }
//        Organization organ = indicatorValue.getCreateOrganization();
//        if (null != organ) {
//            result.setCreateOrganId(organ.getId());
//            result.setCreateOrganName(organ.getName());
//        }
        
        User creator = indicatorValue.getCreator();
        if (null != creator) {
            result.setCreatorId(creator.getId());
            result.setCreatorName(creator.getName());
        }
        return result;
    }
    
    public static IndicatorValue to(IndicatorValueDto dto) {
        IndicatorValue indicatorValue = new IndicatorValue();
        BeanUtils.copyProperties(indicatorValue, dto);
        if (StringUtils.isEmpty(indicatorValue.getId())) {
            indicatorValue.setId(null);
        }
        return indicatorValue;
    }
    
    public static IndicatorValue to(IndicatorValueDto dto, EntityManager entityManager) {
        IndicatorValue indicatorValue = entityManager.find(IndicatorValue.class, dto.getId());
        if(indicatorValue == null) {
        	indicatorValue = new IndicatorValue();
        }
        String[] ignoredProps = { "id","enabled","deleted","createDateTime","deleteDateTime",
				"indicator", "creator", "createOrganization"};
		BeanUtils.copyProperties(indicatorValue, dto, ignoredProps);
//        indicatorValue.setName(dto.getName());
//
//        indicatorValue.setDescription(dto.getDescription());

        
        if (StringUtils.isNotEmpty(dto.getIndicatorId())) {
            indicatorValue.setIndicator(entityManager.find(Indicator.class, dto.getIndicatorId()));
        }
        
        if (StringUtils.isEmpty(dto.getCreatorId())) {
        	indicatorValue.setCreator(entityManager.find(User.class, SecurityUtils.currentUserId()));
		} else {
			indicatorValue.setCreator(entityManager.find(User.class, dto.getCreatorId()));
		}
        
        if (indicatorValue.getCreateOrganization() == null && indicatorValue.getCreator() != null) {
        	indicatorValue.setCreateOrganization(indicatorValue.getCreator().getOrganization());
		}

        if (indicatorValue.getCreateDateTime() == null) {
        	indicatorValue.setCreateDateTime(new Date());
        }        
		
        return indicatorValue;
    }
    
    
    private String indicatorId;
    
    private String indicatorName;
    
    private String creatorId;
    
    private String creatorName;
    
    private String createOrganId;
    
    private String createOrganName;
    
    
	public String getIndicatorId() {
		return indicatorId;
	}

	public void setIndicatorId(String indicatorId) {
		this.indicatorId = indicatorId;
	}

	public String getIndicatorName() {
		return indicatorName;
	}

	public void setIndicatorName(String indicatorName) {
		this.indicatorName = indicatorName;
	}

	public String getCreatorId() {
		return creatorId;
	}

	public void setCreatorId(String creatorId) {
		this.creatorId = creatorId;
	}

	public String getCreatorName() {
		return creatorName;
	}

	public void setCreatorName(String creatorName) {
		this.creatorName = creatorName;
	}

	public String getCreateOrganId() {
		return createOrganId;
	}

	public void setCreateOrganId(String createOrganId) {
		this.createOrganId = createOrganId;
	}

	public String getCreateOrganName() {
		return createOrganName;
	}

	public void setCreateOrganName(String createOrganName) {
		this.createOrganName = createOrganName;
	}
    
}
